class UploadScheduler {
  constructor(options = {}) {
    this.maxConcurrentDownloads = options.maxConcurrentDownloads || 2;
    this.downloadingFiles = []; // 正在下载的文件
    this.successfulFiles = []; // 下载成功的文件
    this.failedFiles = []; // 下载失败的文件
    this.pendingFiles = []; // 等待下载的文件
    this.paused = false; // 是否暂停了所有任务
    this.isDownloading = false; // 是否正在进行下载
  }

  // 添加文件到下载队列
  addFiles(files) {
    files.forEach(file => {
      this.pendingFiles.push({
        file,
        status: 'pending',
      });
    });

    // 尝试启动下载任务
    if (!this.paused && !this.isDownloading) {
      this.start();
    }
  }

  // 开始下载下一个文件
  startNextDownload() {
    if (this.paused || this.downloadingFiles.length >= this.maxConcurrentDownloads || this.pendingFiles.length === 0) return;

    const nextFile = this.pendingFiles.shift();
    if (!nextFile) return;

    this.downloadingFiles.push(nextFile);
    nextFile.status = 'downloading';

    this.downloadFile(nextFile);
  }

  // 下载单个文件
  async downloadFile(fileInfo) {
    try {
      await new Promise((resolve, reject) => setTimeout(() => resolve(), 1000 * Math.random() + 500));
      fileInfo.status = 'success';
      this.successfulFiles.push(fileInfo);
      this.downloadingFiles = this.downloadingFiles.filter(f => f !== fileInfo);

      console.log(`${fileInfo.file.name} downloaded successfully.`);
    } catch (error) {
      fileInfo.status = 'failed';
      this.failedFiles.push(fileInfo);
      this.downloadingFiles = this.downloadingFiles.filter(f => f !== fileInfo);

      console.error(`${fileInfo.file.name} download failed.`);
    } finally {
      // 每次下载任务完成后，检查是否有新的任务可以启动
      this.startNextDownload();
    }
  }

  // 取消或暂停下载
  cancelOrPauseDownload(fileInfo, isCancel = true) {
    if (fileInfo.status === 'downloading') {
      // 实际应用中需要加入取消或暂停下载的具体逻辑
      console.log(`${isCancel ? 'Canceling' : 'Pausing'} download for ${fileInfo.file.name}.`);
      fileInfo.status = isCancel ? 'canceled' : 'paused';
    }
    this.removeFile(fileInfo);
  }

  // 恢复暂停的下载
  resumeDownload(fileInfo) {
    if (fileInfo.status === 'paused') {
      this.pendingFiles.unshift(fileInfo); // 放回等待队列头部
      fileInfo.status = 'pending';
      this.startNextDownload();
    }
  }

  // 移除文件
  removeFile(fileInfo) {
    this.pendingFiles = this.pendingFiles.filter(f => f !== fileInfo);
    this.downloadingFiles = this.downloadingFiles.filter(f => f !== fileInfo);
    this.successfulFiles = this.successfulFiles.filter(f => f !== fileInfo);
    this.failedFiles = this.failedFiles.filter(f => f !== fileInfo);
  }

  // 获取所有文件的状态
  getStatus() {
    return {
      downloading: this.downloadingFiles,
      successful: this.successfulFiles,
      failed: this.failedFiles,
      pending: this.pendingFiles,
    };
  }

  // 清空所有任务
  clearAll() {
    this.pendingFiles = [];
    this.downloadingFiles = [];
    this.successfulFiles = [];
    this.failedFiles = [];
  }

  // 暂停所有任务
  pauseAll() {
    this.paused = true;
  }

  // 恢复所有任务
  resumeAll() {
    this.paused = false;
    this.start();
  }

  // 开始下载
  start() {
    this.isDownloading = true;

    // 启动初始下载任务
    while (this.downloadingFiles.length < this.maxConcurrentDownloads && this.pendingFiles.length > 0) {
      this.startNextDownload();
    }
  }

  // 停止下载
  stop() {
    this.isDownloading = false;
    // 实际应用中可能需要更复杂的逻辑来停止所有正在进行的下载
  }
}

export default UploadScheduler;